package com.dom.stor.modular.provider;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dom.api.stor.StorStockinApi;
import com.dom.common.enums.PubStatEnum;
import com.dom.common.enums.VerfyStatEnum;
import com.dom.common.model.vo.stor.*;
import com.dom.common.utils.BeanUtil;
import com.dom.stor.dao.entity.TsStockInDetailFlow;
import com.dom.stor.dao.service.ITsStockInDetailFlowService;
import com.dom.stor.modular.service.StockinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 入库单-服务者
 * @Author xllei
 * @Date 2018/12/21 2:52 PM
 * @Version 1.0
 **/
@RestController
public class StockinProvider implements StorStockinApi {
    @Autowired
    private StockinService stockinService;
    @Autowired
    private ITsStockInDetailFlowService stockInDetailFlowService;

    @Override
    public S019A findStockinPager(@RequestBody S019R request) {
        //查询入库单
        return stockinService.findStockinPager(request);
    }

    @Override
    public S010A addStockinBill(@RequestBody S010R request) {
        //报文头校验

        //新增入库单与明细
         S010A response = stockinService.addStockinBill(request);

        return response;
    }

    @Override
    public S029A findStockinDetailList(@RequestBody S029R request) {
        Wrapper<TsStockInDetailFlow> wrapper = new EntityWrapper<>();
        wrapper.eq("mem_code", request.getMemCode())
                .and().eq("stock_id", request.getStockId())
                .and().eq("stat", PubStatEnum.STAT_NORMAL.getCode())
                .orderBy("id");

        List<TsStockInDetailFlow> stockInDetailFlows = stockInDetailFlowService.selectList(wrapper);

        List<StockDetailInfo> stockDetailInfos = new ArrayList<>();
        stockInDetailFlows.forEach(entity -> {
            StockDetailInfo stockInDetailFlow = new StockDetailInfo();

            BeanUtil.copyPropertiesIgnoreNull(entity, stockInDetailFlow);

            stockDetailInfos.add(stockInDetailFlow);;
        });

        S029A response = new S029A();
        response.setTotalNum(stockInDetailFlows.size());
        response.setStockDetailData(stockDetailInfos);
        return response;
    }

    @Override
    public S012A editStockinBill(@RequestBody S012R request) {
        return stockinService.updateStockinAndDetail(request);
    }

    @Override
    public S011A delStockinBill(@RequestBody S011R request) {
        return stockinService.delStockinBill(request);
    }

    @Override
    public S015A verfyStockIn(@RequestBody S015R request) {
        request.getStockId();
        Integer verfyStat = request.getVerfyStat();

        if(VerfyStatEnum.VERFY_STAT_PASS.getCode() == verfyStat){
            /**
             * 审核状态为审核通过
             * 1. 新增入库单明细
             * 2. 新增库存信息
             */

            boolean result = stockinService.verfyPassStockIn(request.getOperId(), request.getMemCode(), request.getStockId());
        }else if(VerfyStatEnum.VERFY_STAT_FAIL.getCode() == verfyStat){
            /**
             * 审核不通过
             */
            boolean result = stockinService.verfyNopassStockIn(request.getOperId(), request.getMemCode(), request.getStockId(), request.getVerfyErrMsg());
        }

        //更新入库单流水
        S015A response = new S015A();
        response.setStockId(request.getStockId());
        return response;
    }
}
